package
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import kaaengine.display.BitmapDataRenderer;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;

	[Event(name="images_loaded", type="flash.events.Event")]
	
	
	public class ImageLoader extends EventDispatcher implements IResponder
	{
		public var renderables:ArrayCollection;
		private var num:int = 0;
		private var max:int = 11;
		private var tab:Array;
		private var rep:String;
		private var tabIndex:int = 0;
		public function ImageLoader()
		{			
		}
		
		public function loadImages(_rep:String):void {
			this.rep = _rep;
			loadImage(num);
		}
		public function loadImagesFromArray(_rep:String=null, _tab:Array=null):void {
			if( tab==null) { 
				tab = _tab;
				tabIndex = 0;
			}			
			if(_rep != null ) { 
				rep = _rep;
			}
			var fah :FileAccessHelper = new FileAccessHelper(this);
			fah.loadPNGFile("assets/isometric/"+rep+"/"+tab[tabIndex], false);	
		}
		private function loadImage(num):void {						
			var fah :FileAccessHelper = new FileAccessHelper(this);
			fah.loadPNGFile("assets/isometric/"+rep+"/"+num+".png", false);
		}

		public function result(data:Object):void
		{
			var bitmap:BitmapData = data as BitmapData;
			var bdr:BitmapDataRenderer = new BitmapDataRenderer();
			bdr._bitmap = bitmap;
			renderables.addItem(bdr);
			if( tab==null ) {
				bdr.name = ""+num+".png";
				if( num != max ) {
					num++;
					loadImage(num);
				} else {				
					dispatchEvent(new Event("images_loaded"));				
				}	
			} else {
				bdr.name = tab[tabIndex];
				trace("bdr.name="+bdr.name);
				tabIndex++;
				if( tabIndex != tab.length ) {					
					loadImagesFromArray();
				} else {				
					dispatchEvent(new Event("images_loaded"));				
				}				
			}
						
			
					
		}
		
		public function fault(info:Object):void
		{
		}
		
	}
}